﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using PM.Services;
using PM.Entity;
using System.Collections.Generic;


namespace PM.Site.Admin
{
    public partial class BankTransactions : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserName"] == null || Session["UserTypeId"] == null)
            {
                Response.Redirect("Login.aspx?RequestUrl=" + Request.Url.OriginalString);
            }
            else if (Session["UserTypeId"] != null)
            {
                if (Int32.Parse(Session["UserTypeId"].ToString()) == 1)
                {
                    this.Loading();
                }
                else
                {
                    Response.Redirect("Default.aspx?RequestUrl=" + Request.Url.OriginalString);
                }

            }
            else
            {
                Response.Redirect("Default.aspx?RequestUrl=" + Request.Url.OriginalString);
            }
        }

        private void Loading()
        {
            if (!Page.IsPostBack)
            {
                ddlAccNos.DataSource = BankDepositServices.GetAccNoCollection();
                ddlAccNos.DataBind();
                ddlDebitCredit.DataSource = BankTransactionServices.GetDebitCreditCollection();
                ddlDebitCredit.DataBind();
                BankTransactionCollection btc = BankTransactionServices.GetBankTransCollection();
                dlBankTrans.DataSource = btc;
                dlBankTrans.DataBind();

                ddlAccountNos.DataSource = BankDepositServices.GetAccNoCollection();
                ddlAccountNos.AppendDataBoundItems = true;
                ddlAccountNos.DataBind();
                
            }
        }

        protected void dlBankTrans_ItemCommand(object source, DataListCommandEventArgs e)
        {
            string transactionId = e.CommandArgument.ToString();
            switch (e.CommandName)
            {
                /*case "DeleteUserAccount":
                    UserAccountServices.Delete(userName);
                    Response.Redirect("~/UsersForm.aspx");
                    break;*/

                case "EditBankTrans":
                    this.LoadBankTransByTransId(transactionId);
                    break;
            }

        }

        private void LoadBankTransByTransId(string transactionId)
        {
            if (!Page.IsPostBack)
            {
                ddlAccNos.DataSource = BankDepositServices.GetAccNoCollection();
                ddlAccNos.DataBind();
                ddlDebitCredit.DataSource = BankTransactionServices.GetDebitCreditCollection();
                ddlDebitCredit.DataBind();
            }
            BankTransaction bt = BankTransactionServices.GetBankTransactionByTransId(transactionId);
            txbTransId.Text = bt.TransactionId.ToString();
            ddlAccNos.SelectedValue = bt.AccNo;
            ddlDebitCredit.SelectedValue = bt.DebitCredit;
            txbAmount.Text = bt.Amount.ToString();
            txbTransDate.Text = bt.TransDate.ToShortDateString();
            chkbPosted.Checked = bt.AccPosted;

        }

        protected void btnSave_Click(object sender, EventArgs e)
        {

            BankTransaction bt = new BankTransaction();
            if (txbTransId.Text != string.Empty)
            {
                bt.TransactionId = Int32.Parse(txbTransId.Text);
            }
            bt.AccNo = ddlAccNos.SelectedItem.Value;
            bt.DebitCredit = ddlDebitCredit.SelectedItem.Value;
            decimal amount = 0.0M;
            decimal.TryParse(txbAmount.Text, out amount);
            bt.Amount = amount;
            DateTime transDate = DateTime.MinValue;
            if (DateTime.TryParse(txbTransDate.Text, out transDate))
            {
                bt.TransDate = transDate;
            }
            bt.AccPosted = chkbPosted.Checked;
            BankTransactionServices.Add(bt);
            Response.Redirect("~/Admin/BankTransactions.aspx");
        }

        protected void btnNew_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Admin/BankTransactions.aspx");
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {                      
            foreach (DataListItem item in dlBankTrans.Items)
            {
                CheckBox chkPosted = (CheckBox)item.FindControl("chkPosted");
                if (chkPosted.Enabled && chkPosted.Checked)
                {
                    string transId = dlBankTrans.DataKeys[item.ItemIndex].ToString();
                    BankTransaction bt = BankTransactionServices.GetBankTransactionByTransId(transId);
                    bt.AccPosted = true;
                    BankTransactionServices.Update(bt);
                }
            }

            Response.Redirect("~/Admin/BankTransactions.aspx");
        }

        protected void ddlAccountNos_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlAccountNos.SelectedValue != string.Empty)
            {
                dlBankTrans.DataSource = BankTransactionServices.GetBankTransactionCollectionByAccNo(ddlAccountNos.SelectedValue);
                dlBankTrans.DataBind();
            }
            else
            {
                dlBankTrans.DataSource = BankTransactionServices.GetBankTransCollection();
                dlBankTrans.DataBind();
            }
        }
    }
}
